package com.zeroup.auto.assists.step.unify;

import android.graphics.Path;
import android.graphics.Rect;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ScreenUtils;
import com.ven.assists.Assists;
import com.ven.assists.AssistsWindowManager;
import com.ven.assists.stepper.Step;
import com.ven.assists.stepper.StepCollector;
import com.zeroup.auto.assists.DouYinBaseStep;
import com.zeroup.auto.assists.kt.MyContinuation;
import com.zeroup.auto.assists.next.StepBeanData;
import com.zeroup.auto.assists.step.unify.bean.DyFosterValue;
import com.zeroup.auto.utils.AssistsUtils;
import com.zeroup.auto.utils.MyIntPredicate;
import com.zeroup.auto.utils.RandomUtile;
import com.zeroup.auto.utils.ZeroUpStrUtile;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.IntPredicate;

/**
 * 抖音养号
 */
public class DyFosterStep extends DouYinBaseStep {

    private DyFosterValue dyFosterValue;

    private AtomicInteger like = new AtomicInteger(0);
    private AtomicInteger total = new AtomicInteger(0);



    @Override
    public void onImpl(@NonNull StepCollector stepCollector) {
        stepCollector.next(1,false,(i,c)->{//  进入 指定列表
            Object serviceData = AssistsUtils.getServiceData(i.getData());
            if(serviceData==null){
                return AssistsUtils.nextStep( i.getData());
            }

            if(serviceData instanceof StepBeanData){
                StepBeanData stepBeanData = (StepBeanData)serviceData;
                this.dyFosterValue = JSON.parseObject(stepBeanData.getDataValue(),DyFosterValue.class);
                if(dyFosterValue.getYhms()==1){
                    List<AccessibilityNodeInfo> infoList = Assists.INSTANCE.findByText("推荐");
                    while (infoList.isEmpty()){
                        Assists.INSTANCE.back();
                        infoList = Assists.INSTANCE.findByText("推荐");
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }else if(dyFosterValue.getYhms()==2){

                    MyContinuation myContinuation = new MyContinuation();
                    Assists.INSTANCE.findByText("首页").forEach(it->{  //回到首页
                        Rect rect = new Rect();
                        it.getBoundsInScreen(rect);
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, myContinuation);
                    });
                    Assists.INSTANCE.findByText("推荐").forEach(it->{ //根据推荐位置 滑动tab
                        Rect rect = new Rect();
                        it.getBoundsInScreen(rect);
                        Path mPath=new Path();//线性的path代表手势路径,点代表按下,封闭的没用
                        mPath.moveTo(rect.centerX(), rect.centerY());
                        mPath.lineTo(rect.centerX()+150, rect.centerY());
                        AssistsWindowManager.INSTANCE.switchNotTouchableAll();
                        Assists.gesture(mPath,0,300, myContinuation); //右滑
                        AssistsWindowManager.INSTANCE.switchTouchableAll();
                    });
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    List<AccessibilityNodeInfo> infoList = Assists.INSTANCE.findByText("直播");
                    infoList.forEach(it->{
                        Rect rect = new Rect();
                        it.getBoundsInScreen(rect);
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, myContinuation);
                    });
                }
            }
            return  Step.Companion.get(2,this.getClass(),serviceData,2000);
        }).next(2,false,(i,c)->{//滑动
            Path mPath=new Path();//线性的path代表手势路径,点代表按下,封闭的没用
            AssistsWindowManager.INSTANCE.switchNotTouchableAll();
            MyContinuation myContinuation = new MyContinuation();
            mPath.moveTo(500, 1000);
            mPath.lineTo(500, 400);
            Assists.gesture(mPath,0,300, myContinuation); //上滑
            AssistsWindowManager.INSTANCE.switchTouchableAll();
            return  Step.Companion.get(3,this.getClass(),i.getData(),2000);
        }).next(3,false,(i,c)->{// 检测内容
            StringBuffer stringBuffer = new StringBuffer();
            List<AccessibilityNodeInfo> remarks = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/xtv");
            remarks.forEach(it->{
                stringBuffer.append(it.getText());
            });
            List<AccessibilityNodeInfo> nickNames = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/0dy");
            nickNames.forEach(it->{
                stringBuffer.append(it.getText());
            });
            String content = stringBuffer.toString();
            String  value = dyFosterValue.getKeyword();
            List<String> keywords = ZeroUpStrUtile.spString(value);
            AtomicInteger b = new AtomicInteger(0);
            keywords.forEach(k->{
                if(content.contains(k)){
                    b.getAndIncrement();
                }
            });
            if(b.get() > 0){//有匹配的关键字
                total.getAndIncrement();//  刷到的总数
                return  Step.Companion.get(4,this.getClass(),i.getData(),2000);//重复滑动
            }else{
                Date now = new Date();
                if(now.before(dyFosterValue.getEndTime())){//未到截止时间
                    return  Step.Companion.get(2,this.getClass(),i.getData(),2000);//重复滑动
                }else{
                    return Step.Companion.getNone();
                }
            }
        }).next(4,false,(i,c)->{ //关注
            int [] fllowtypes = dyFosterValue.getFlowTypes();
            List<AccessibilityNodeInfo> isZhibo = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/nn1");//判断是否在直播
            if(dyFosterValue.getYhms()==1){
                if(isZhibo.isEmpty()){ //非直播关注
                    boolean isFlow = Arrays.stream(fllowtypes).allMatch(new MyIntPredicate(1));
                    if(isFlow){
                        Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/ia4").forEach(it->{  //直接点击头像上的关注按钮
                            Rect rect = new Rect();
                            it.getBoundsInScreen(rect);
                            Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, new MyContinuation());
                        });
                    }
                }else{//直播关注
                    Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/0dy").forEach(it->{  //先点击头像进入
                        Rect rect = new Rect();
                        it.getBoundsInScreen(rect);
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, new MyContinuation());
                    });

                    Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/sxm").forEach(it->{  //再点击关注按钮
                        Rect rect = new Rect();
                        it.getBoundsInScreen(rect);
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 1000, new MyContinuation());
                    });
                    Assists.INSTANCE.back(); //返回到列表
                }
            }else if(dyFosterValue.getYhms()==2){
                boolean isFlow = Arrays.stream(fllowtypes).allMatch(new MyIntPredicate(1));
                boolean isFlow2 = Arrays.stream(fllowtypes).allMatch(new MyIntPredicate(2));
                boolean isFlow3 = Arrays.stream(fllowtypes).allMatch(new MyIntPredicate(3));
                if(isFlow){
                    Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/0dy").forEach(it->{  //先点击头像进入
                        Rect rect = new Rect();
                        it.getBoundsInScreen(rect);
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, new MyContinuation());
                    });

                    Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/sxm").forEach(it->{  //再点击关注按钮
                        Rect rect = new Rect();
                        it.getBoundsInScreen(rect);
                        Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 1000, new MyContinuation());
                    });
                    Assists.INSTANCE.back(); //返回到列表
                }else{
//                    List<AccessibilityNodeInfo> isfudai = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/nn1");//判断是否有福袋
//                    if(isFlow2 && !isfudai.isEmpty()){//是否 福袋主播
//                        Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/0dy").forEach(it->{  //先点击头像进入
//                            Rect rect = new Rect();
//                            it.getBoundsInScreen(rect);
//                            Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, new MyContinuation());
//                        });
//
//                        Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/sxm").forEach(it->{  //再点击关注按钮
//                            Rect rect = new Rect();
//                            it.getBoundsInScreen(rect);
//                            Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 1000, new MyContinuation());
//                        });
//                        Assists.INSTANCE.back(); //返回到列表
//                    }else{
//                        List<AccessibilityNodeInfo> ishb = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/nn1");//判断是否有红包
//                        if(isFlow3 && !ishb.isEmpty()){//是否 红包
//                            Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/0dy").forEach(it->{  //先点击头像进入
//                                Rect rect = new Rect();
//                                it.getBoundsInScreen(rect);
//                                Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 100, new MyContinuation());
//                            });
//
//                            Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/sxm").forEach(it->{  //再点击关注按钮
//                                Rect rect = new Rect();
//                                it.getBoundsInScreen(rect);
//                                Assists.INSTANCE.gestureClick(rect.centerX(), rect.centerY(), 1000, new MyContinuation());
//                            });
//                            Assists.INSTANCE.back(); //返回到列表
//                        }
//                    }
                }

            }
            return  Step.Companion.get(5,this.getClass(),i.getData(),2000);//进入判断是否点赞
        }).next(5,false,(i,c)->{ //点赞
            List<AccessibilityNodeInfo> isZhibo = Assists.INSTANCE.findById("com.ss.android.ugc.aweme:id/nn1");//判断是否在直播
            if(isZhibo.isEmpty()){//非直播
                if((like.get()/total.get() *100) < 50){
                    MyContinuation myContinuation = new MyContinuation();
                    int x = ScreenUtils.getAppScreenWidth() / 2;
                    int y = ScreenUtils.getAppScreenHeight() / 2;
                    Path mPath=new Path();
                    mPath.moveTo(x, y);
                    mPath.lineTo(x, y);
                    this.runMain(j->{
                        AssistsWindowManager.INSTANCE.switchNotTouchableAll();
                        Assists.gesture(mPath,0,1,myContinuation);
                        Assists.gesture(mPath,0,1,myContinuation);
                        AssistsWindowManager.INSTANCE.switchTouchableAll();
                        return null;
                    },myContinuation);
                    like.getAndIncrement();//点赞
                }
            }
            return  Step.Companion.get(6,this.getClass(),i.getData(),2000);//进入是否等待
        }).next(6,false,(i,c)->{ //停顿
            try {
                long stopSend = RandomUtile.rangLong(dyFosterValue.getRandmonStopTime()-5,dyFosterValue.getRandmonStopTime()+5);
                Thread.sleep(stopSend * 1000);
                Date now = new Date();
                if(now.before(dyFosterValue.getEndTime())){//未到截止时间
                    return  Step.Companion.get(2,this.getClass(),i.getData(),2000);//重复滑动
                }else{
                    return Step.Companion.getNone();
                }
            } catch (InterruptedException e) {
                return  Step.Companion.get(2,this.getClass(),i.getData(),2000);//重复滑动
            }
        });
    }
}
